//==============================================================================
//
//                   GPXPlot - the gpx plotter class
//
//               Copyright (C) 2018  Dick van Oudheusden
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//==============================================================================

#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <ctime>
#include <iomanip>
#include <limits>
#include <algorithm>

#include "Arguments.h"
#include "XMLParser.h"
#include "Algorithm.h"

namespace gpxtools
{
  // -- PlotAxis interface ------------------------------------------
  class PlotAxis
  {
  public:
    PlotAxis() { }
    virtual ~PlotAxis() {}

    virtual bool values(const std::string &latitude, const std::string &longitude, const std::string &elevation, const std::string &timestamp) = 0;
    virtual void rollback() = 0;

    virtual void heading(std::ostream &str, const std::string &xyz) const = 0;
    virtual bool start() = 0;
    virtual bool next(std::ostream &str) = 0;
  };

  // -- DistanceAxis ------------------------------------------------
  class DistanceAxis : public PlotAxis
  {
  public:
    DistanceAxis() :
      PlotAxis(),
      _prevLat(0.0),
      _prevLon(0.0) { }
    ~DistanceAxis() { }

    virtual bool values(const std::string &latitude, const std::string &longitude, const std::string &, const std::string &)
    {
      if (latitude.empty())  return false;
      if (longitude.empty()) return false;

      double lat;
      double lon;

      try
      {
        lat = std::stod(latitude);
        lon = std::stod(longitude);
      }
      catch(...)
      {
        return false;
      }

      if (_distances.empty())
      {
        _distances.push_back(0.0);
      }
      else
      {
        _distances.push_back(_distances.back() + gpx::calcDistance(_prevLat, _prevLon, lat, lon)); // Cumulative distance
      }

      _prevLat = lat;
      _prevLon = lon;

      return true;
    }

    virtual void rollback()
    {
      _distances.pop_back();
    }

    virtual void heading(std::ostream &str, const std::string &xyz) const
    {
      str << "set " << xyz << "label \"Distance (m)\"" << std::endl;
      str << "set " << xyz << "range ["
          << std::fixed << std::setprecision(1) << _distances.front() << ':'
          << std::fixed << std::setprecision(1) << _distances.back() << ']' << std::endl;
    }

    virtual bool start()
    {
      _current = _distances.begin();

      return (_current != _distances.end());
    }

    virtual bool next(std::ostream &str)
    {
      if (_current == _distances.end()) return false;

      str << std::fixed << std::setprecision(1) << *_current;

      _current++;

      return true;
    }

  private:
    double _prevLat;
    double _prevLon;

    std::vector<double>           _distances;
    std::vector<double>::iterator _current;
  };

  // -- ElevationAxis -----------------------------------------------
  class ElevationAxis : public PlotAxis
  {
  public:
    ElevationAxis() :
      PlotAxis() { }
    ~ElevationAxis() { }

    virtual bool values(const std::string &, const std::string &, const std::string &elevation, const std::string &)
    {
      if (elevation.empty()) return false;

      double ele;

      try
      {
        ele = std::stod(elevation);
      }
      catch(...)
      {
        return false;
      }

      _elevations.push_back(ele);

      return true;
    }

    virtual void rollback()
    {
      _elevations.pop_back();
    }

    virtual void heading(std::ostream &str, const std::string &xyz) const
    {
      str << "set " << xyz << "label \"Elevation (m)\"" << std::endl;

      double minEle =  std::numeric_limits<double>::max();
      double maxEle = -std::numeric_limits<double>::max();
      for (auto elevation = _elevations.begin(); elevation != _elevations.end(); ++elevation)
      {
        minEle = std::min(minEle, *elevation);
        maxEle = std::max(maxEle, *elevation);
      }
      str << "set " << xyz << "range ["
          << std::fixed << std::setprecision(1) << minEle << ':'
          << std::fixed << std::setprecision(1) << maxEle << ']' << std::endl;
    }

    virtual bool start()
    {
      _current = _elevations.begin();

      return (_current != _elevations.end());
    }

    virtual bool next(std::ostream &str)
    {
      if (_current == _elevations.end()) return false;

      str << std::fixed << std::setprecision(1) << *_current;

      _current++;

      return true;
    }

  private:
    std::vector<double>           _elevations;
    std::vector<double>::iterator _current;
  };

  // -- SpeedAxis ------------------------------------------------
  class SpeedAxis : public PlotAxis
  {
  public:
    SpeedAxis() :
      PlotAxis(),
      _prevLat(0.0),
      _prevLon(0.0),
      _prevTime(-1) { }
    ~SpeedAxis() { }

    virtual bool values(const std::string &latitude, const std::string &longitude, const std::string &, const std::string &timestamp)
    {
      if (latitude.empty()) return false;
      if (longitude.empty()) return false;
      if (timestamp.empty()) return false;

      time_t time;
      double lat;
      double lon;

      // Time format: 2009-10-17T18:37:31Z
      struct tm  fields = {0};

      if (strptime(timestamp.c_str(), "%Y-%m-%dT%TZ", &fields) == nullptr) return false;

      time = mktime(&fields);

      if (time == time_t(-1)) return false;

      try
      {
        lat = std::stod(latitude);
        lon = std::stod(longitude);
      }
      catch(...)
      {
        return false;
      }

      if (_speeds.empty())
      {
        _speeds.push_back(0.0);
      }
      else
      {
        double distance = gpx::calcDistance(_prevLat, _prevLon, lat, lon);

        if ((_prevTime != time_t(-1)) && (time != _prevTime))
        {
          _speeds.push_back((distance / difftime(time, _prevTime)) * 3.6);
        }
        else
        {
          _speeds.push_back(0.0);
        }
      }

      _prevLat  = lat;
      _prevLon  = lon;
      _prevTime = time;

      return true;
    }

    virtual void rollback()
    {
      _speeds.pop_back();
    }

    virtual void heading(std::ostream &str, const std::string &xyz) const
    {
      str << "set " << xyz << "label \"Speed (km/h)\"" << std::endl;

      double minSpeed =  std::numeric_limits<double>::max();
      double maxSpeed = -std::numeric_limits<double>::max();
      for (auto speed = _speeds.begin(); speed != _speeds.end(); ++speed)
      {
        minSpeed = std::min(minSpeed, *speed);
        maxSpeed = std::max(maxSpeed, *speed);
      }
      str << "set " << xyz << "range ["
          << std::fixed << std::setprecision(1) << minSpeed << ':'
          << std::fixed << std::setprecision(1) << maxSpeed << ']' << std::endl;
    }

    virtual bool start()
    {
      _current = _speeds.begin();

      return (_current != _speeds.end());
    }

    virtual bool next(std::ostream &str)
    {
      if (_current == _speeds.end()) return false;

      str << std::fixed << std::setprecision(1) << *_current;

      _current++;

      return true;
    }

  private:
    double _prevLat;
    double _prevLon;
    time_t _prevTime;

    std::vector<double>           _speeds;
    std::vector<double>::iterator _current;
  };

  // -- TimeAxis ------------------------------------------------
  class TimeAxis : public PlotAxis
  {
  public:
    TimeAxis() :
      PlotAxis() { }
    ~TimeAxis() { }

    virtual bool values(const std::string &, const std::string &, const std::string &, const std::string &timestamp)
    {
      time_t time;

      // Time format: 2009-10-17T18:37:31Z
      if (timestamp.empty()) return false;

      struct tm  fields = {0};

      if (strptime(timestamp.c_str(), "%Y-%m-%dT%TZ", &fields) == nullptr) return false;

      time = timegm(&fields); // trick: gnuplot uses UTC

      if (time == time_t(-1)) return false;

      _times.push_back(time);

      return true;
    }

    virtual void rollback()
    {
      _times.pop_back();
    }

    virtual void heading(std::ostream &str, const std::string &xyz) const
    {
      str << "set " << xyz << "label \"Time\"" << std::endl;
      str << "set timefmt \"" <<  "%s" << "\"" << std::endl;
      str << "set " << xyz << "data time" << std::endl;
      str << "set format " << xyz << " \"%H:%M\"" << std::endl;
      str << "set " << xyz << "range [\"" << _times.front() << "\":\"" << _times.back() << "\"]" << std::endl;
    }

    virtual bool start()
    {
      _current = _times.begin();

      return (_current != _times.end());
    }

    virtual bool next(std::ostream &str)
    {
      if (_current == _times.end()) return false;

      str << *_current;

      _current++;

      return true;
    }

  private:
    std::vector<time_t>           _times;
    std::vector<time_t>::iterator _current;
  };

  // -- GPXPlot -----------------------------------------------------
  class GPXPlot : public XMLParserHandler
  {
  public:
    GPXPlot() :
      _arguments("gpxplot [OPTION].. [FILE]\nGenerate a GnuPlot command file from a GPX-file on standard output.\n", "gpxplot v0.1",
                 "Generate a GnuPlot command file from the distance, elevation, speed and time in a GPX-file on standard output."),
      _title  (_arguments, true,  't', "title",   "TITLE",          "set the title of the plot", ""),
      _xTitle (_arguments, true,  'x', "x",       "SOURCE",         "set the x-axis source (distance, time) (def:time)", "time"),
      _yTitle (_arguments, true,  'y', "y",       "SOURCE",         "set the y-axis source (elevation, distance, speed) (def:speed)", "speed"),
      _style  (_arguments, true,  'p', "plot",    "STYLE",          "set the plot style (lines,..) (def:lines)", "lines"),
      _color  (_arguments, true,  'c', "color",   "COLOR",          "set the line color", ""),
      _term   (_arguments, true, 'm', "term",     "x11|qt|..",      "set the output device and options", ""),
      _width  (_arguments, true,  'w', "width",   "WIDTH",          "set the width of the plot", "1024"),
      _height (_arguments, true,  'h', "height",  "HEIGHT",         "set the height of the plot", "768"),
      _output (_arguments, true,  'o', "output",  "FILE",           "set the output file of the plot", ""),
      _xmlParser(this),
      _xAxis(nullptr),
      _yAxis(nullptr)
    {
    }

    ~GPXPlot()
    {

    }

    bool parseArguments(int argc, char *argv[])
    {
      std::vector<std::string> filenames;

      if (!_arguments.parse(argc,argv, filenames))
      {
        return false;
      }
      else if (!checkArguments(filenames))
      {
        return false;
      }
      else if (filenames.empty())
      {
        return _xmlParser.parse(std::cin);
      }
      else
      {
        return parseFile(filenames.front());
      }
    }

    bool checkArguments(const std::vector<std::string> &filenames)
    {
      if (filenames.size() > 1)
      {
        std::cerr << "Too many input files."  << std::endl;
        return false;
      }

      if (_xTitle.value() == "distance")
      {
        _xAxis = new DistanceAxis();
      }
      else if (_xTitle.value() == "time")
      {
        _xAxis = new TimeAxis();
      }
      else
      {
        std::cerr << "Invalid value for --" << _xTitle.longOption() << ": " << _xTitle.value() << std::endl;
        return false;
      }

      if (_yTitle.value() == "elevation")
      {
        _yAxis = new ElevationAxis();
      }
      else if (_yTitle.value() == "distance")
      {
        _yAxis = new DistanceAxis();
      }
      else if (_yTitle.value() == "speed")
      {
        _yAxis = new SpeedAxis();
      }
      else
      {
        std::cerr << "Invalid value for --" << _yTitle.longOption() << ": " << _yTitle.value() << std::endl;
        return false;
      }

      if (_style.value() != "lines")
      {
        std::cerr << "Invalid value for --" << _style.longOption() << ": " << _style.value() << std::endl;
        return false;
      }

      return true;
    }

    bool parseFile(const std::string &filename)
    {
      bool ok =false;

      std::ifstream file(filename);

      if (file.is_open())
      {
        ok = _xmlParser.parse(file);

        file.close();
      }
      else
      {
        std::cerr << "Unable to open: " << filename << std::endl;
      }

      return ok;
    }

    void generate(std::ostream &output)
    {
      if ((!_xAxis->start()) || (!_yAxis->start()))
      {
        std::cerr << "No values present for the plot." << std::endl;
        return;
      }

      // Export the data
      output << "$DATA << EOD" << std::endl;

      while(true)
      {
        if (!_xAxis->next(output)) break;
        output << ' ';
        if (!_yAxis->next(output)) break;
        output << std::endl;
      }
      output << "EOD" <<std::endl;

      if (!_title.value().empty())
        output << "set title \"" << _title.value() << "\"" << std::endl;

      output << "unset key" << std::endl;

      _xAxis->heading(output, "x");
      _yAxis->heading(output, "y");

      if (!_output.value().empty())
      {
        output << "set output \"" << _output.value() << "\"" << std::endl;

        if (!_term.value().empty())
        {
          output << "set term " << _term.value() << std::endl;
        }
        else
        {
          std::size_t pos = _output.value().find_last_of('.');

          if (pos != std::string::npos)
          {
            std::string extension = _output.value().substr(pos+1);

            std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);

            if (extension == "jpg")
            {
              extension = "jpeg";
            }

            output << "set term " << extension;

            if ((!_width.value().empty()) && (!_height.value().empty()))
            {
              output << " size " << _width.value() << ',' << _height.value();
            }
            output << std::endl;
          }
        }
      }
      else if (!_term.value().empty())
      {
        output << "set term " << _term.value() << std::endl;
      }

      output << "plot $DATA using 1:2 with " << _style.value();
      if (!_color.value().empty())
      {
        output << " lt rgb \"" << _color.value() << "\"";
      }
      output << std::endl;

      if (_output.value().empty())
      {
        output << "pause -1" << std::endl;
      }
    }

    virtual void startElement(const std::string &path, const std::string &, const Attributes &attributes)
    {
      if (path == "/gpx/trk/trkseg/trkpt")
      {
        _lat.clear();
        _lon.clear();
        _elevation.clear();
        _time.clear();

        _lat = fetch(attributes, "lat");
        _lon = fetch(attributes, "lon");
      }
    }

    virtual void text(const std::string &path, const std::string &text)
    {
      if (path == "/gpx/trk/trkseg/trkpt/ele")
      {
        _elevation.append(text);
      }
      if (path == "/gpx/trk/trkseg/trkpt/time")
      {
        _time.append(text);
      }
    }

    virtual void endElement(const std::string &path, const std::string &)
    {
      if (path == "/gpx/trk/trkseg/trkpt")
      {
        XMLParser::trim(_lat);
        XMLParser::trim(_lon);
        XMLParser::trim(_elevation);
        XMLParser::trim(_time);

        if (_xAxis->values(_lat, _lon, _elevation, _time))
        {
          if (!_yAxis->values(_lat, _lon, _elevation, _time))
          {
            _xAxis->rollback();
          }
        }
      }
    }

  private:
    std::string fetch(const Attributes &attributes, const std::string &key)
    {
      auto attribute = attributes.find(key);

      return (attribute != attributes.end() ? attribute->second : "");
    }

  private:
    arg::Arguments           _arguments;

    arg::Argument            _title;
    arg::Argument            _xTitle;
    arg::Argument            _yTitle;
    arg::Argument            _style;
    arg::Argument            _color;
    arg::Argument            _term;
    arg::Argument            _width;
    arg::Argument            _height;
    arg::Argument            _output;

    XMLParser                _xmlParser;

    std::string              _lat;
    std::string              _lon;
    std::string              _elevation;
    std::string              _time;

    PlotAxis                *_xAxis;
    PlotAxis                *_yAxis;
  };
}


int main(int argc, char *argv[])
{
  gpxtools::GPXPlot gpxPlot;

  if (gpxPlot.parseArguments(argc, argv))
  {
    gpxPlot.generate(std::cout);

    return 0;
  }

  return 1;
}
